package com.study.lease.web.admin.custom.config;

import com.study.lease.web.admin.custom.converter.StringToBaseEnumConverterFactory;

import com.study.lease.web.admin.custom.interceptor.AuthenticationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * description:注册类型转换器Converter
 */
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
    /**
     * 这里想要注入，需要在StringToItemTypeConverter类中添加@Comment，使他成为一个Bean
     * 注册类型转换器，这里使用工厂模式，因为枚举类中存在多个枚举类型，所以使用工厂模式
     */
   /* @Autowired
    private StringToItemTypeConverter stringToItemTypeConverter;*/
    @Autowired
    private StringToBaseEnumConverterFactory stringToBaseEnumConverterFactory;

    @Autowired
    private AuthenticationInterceptor authenticationInterceptor;

    /**
     * 注册类型转换器
     * @param registry
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
      /*  registry.addConverter(this.stringToItemTypeConverter);*/
        registry.addConverterFactory(this.stringToBaseEnumConverterFactory);
    }

    /**
     * 登录验证拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        /**
         * 添加认证拦截器
         * 拦截所有请求，除了登录请求
         */
        registry.addInterceptor(this.authenticationInterceptor).addPathPatterns("/admin/**").excludePathPatterns("/admin/login/**");
    }
}